package com.designpattern.obeserver.method1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: guoqiang.lv
 * @date: 2019/6/18
 * @description:
 */
public class WeatherData implements Subject {

    private List<Obeserver> obeservers;

    private float temperature;

    private float humidity;

    private float pressure;

    public WeatherData() {
        this.obeservers = new ArrayList<>();
    }

    @Override
    public void registerObeserver(Obeserver obeserver) {
        if (!obeservers.contains(obeserver)) {
            obeservers.add(obeserver);
        }
    }

    @Override
    public void deleteObeserver(Obeserver obeserver) {
        obeservers.remove(obeserver);
    }

    @Override
    public void notifyObservers() {
        obeservers.forEach((obeserver) -> obeserver.update(this));
    }

    public void measurementsChanged() {
        notifyObservers();
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
    }

    public float getTemperature() {
        return temperature;
    }

    public float getHumidity() {
        return humidity;
    }

    public float getPressure() {
        return pressure;
    }
}
